﻿Imports Oracle.DataAccess.Client
Public Class cCaja
    Private drCaja As dsCaja.CAJ_CAJASRow
    Dim daCaja As dsCajaTableAdapters.CAJ_CAJASTableAdapter
    Dim dtCaja As dsCaja.CAJ_CAJASDataTable
    Private iSucursal As cSedes
    Public Sub New(ByVal IdCaja As String)
        Try
            Dim daCaja As New dsCajaTableAdapters.CAJ_CAJASTableAdapter
            dtCaja = daCaja.GetDataByIdCaja(IdCaja)
            If dtCaja.Rows.Count > 0 Then
                drCaja = dtCaja.Rows(0)
                iSucursal = New cSedes(drCaja.IDSUCURSAL)
            Else
                Throw New Exception("Caja no existe favor verificar e intentar de nuevo")
            End If
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Public ReadOnly Property IdCaja() As String
        Get
            Return drCaja.IDCAJA
        End Get
    End Property
    Public ReadOnly Property Descripcion() As String
        Get
            Return drCaja.DESCRIPCION
        End Get
    End Property
    Public ReadOnly Property Sucursal() As cSedes
        Get
            Return iSucursal
        End Get
    End Property
    Public Property FechaUltimoCuadre() As Date
        Set(ByVal value As Date)
            drCaja.FECHAULTCUADRE = value
        End Set
        Get
            If drCaja.IsFECHAULTCUADRENull Then
                Return Date.Today.AddYears(-100)
            Else
                Return drCaja.FECHAULTCUADRE
            End If
        End Get
    End Property
    Public Property Saldo() As Double
        Set(ByVal value As Double)
            drCaja.SALDO = value
        End Set
        Get
            Return drCaja.SALDO
        End Get
    End Property
    Public Sub Salvar()
        Try
            daCaja.Update(dtCaja)
        Catch ex As Exception
            Throw ex
        End Try
    End Sub
End Class

Public Class TipoGasto
    Private drTipoGasto As dsCaja.CAJ_TIPOGASTORow
    Public Sub New(ByVal IdTipo As String)
        Try
            Dim daTipoGas As New dsCajaTableAdapters.CAJ_TIPOGASTOTableAdapter
            Dim dtTipoGas As dsCaja.CAJ_TIPOGASTODataTable = daTipoGas.GetDataByidTipoGasto(IdTipo)
            If dtTipoGas.Rows.Count > 0 Then
                drTipoGasto = dtTipoGas.Rows(0)
            Else
                Throw New Exception("Tipo Gasto no existe favor verificar e intentar de nuevo")
            End If
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Public ReadOnly Property idTipoGasto() As String
        Get
            Return drTipoGasto.IDGASTO
        End Get
    End Property
    Public ReadOnly Property Descricion() As String
        Get
            Return drTipoGasto.DESCRIPCION
        End Get
    End Property
End Class

Public Class cCuadreCaja
    Private iCaja As cCaja
    Private lSaldoInicial As Double = 0
    Private lTotalGastos As Double = 0
    Private lTotalRC As Double = 0
    Private lTotalConsignaciones As Double = 0
    Private lTotalVentasContado As Double = 0
    Private lFechaCuadre As Date
    Private lIdCaja As String
    Private lDsRc As dsConsultaCartera
    Private Sub GetRecibosCaja()
        Using conn As OracleConnection = New OracleConnection(My.Settings.ConnectionString)
            Using cmd As OracleCommand = conn.CreateCommand
                cmd.CommandText = "SELECT IDDOCUMENTO, SUM(VALORRECIBO) AS VALORRECIBO FROM CAR_RECIBOSCAJA WHERE TRUNC(FECHA)=:PFECHA GROUP BY IDDOCUMENTO"
                cmd.Parameters.Add("PFECHA", OracleDbType.Date).Value = lFechaCuadre
                Try
                    conn.Open()
                    lDsRc.CAR_RCCUADRECAJA.Load(cmd.ExecuteReader)
                    conn.Close()
                Catch ex As Exception
                    Throw ex
                End Try
            End Using
        End Using
    End Sub
    Private Sub CargaDatosCuadre()
        Using conn As OracleConnection = New OracleConnection(My.Settings.ConnectionString)
            Using cmd As OracleCommand = conn.CreateCommand
                cmd.CommandText = "CAJ_CUADRECAJA"
                cmd.CommandType = CommandType.StoredProcedure
                cmd.Parameters.Add("PFECHACUADRE", OracleDbType.Date).Value = lFechaCuadre
                cmd.Parameters.Add("PIDCAJA", OracleDbType.Varchar2).Value = lIdCaja
                Dim SaldoIni As OracleParameter = cmd.Parameters.Add("PSALDOINICIAL", OracleDbType.Double)
                SaldoIni.Direction = ParameterDirection.Output
                Dim Consigna As OracleParameter = cmd.Parameters.Add("PTOTCONSIGNA", OracleDbType.Double)
                Consigna.Direction = ParameterDirection.Output
                Dim Gasto As OracleParameter = cmd.Parameters.Add("PTOTGASTO", OracleDbType.Double)
                Gasto.Direction = ParameterDirection.Output
                Dim RecibosCaja As OracleParameter = cmd.Parameters.Add("PTOTRC", OracleDbType.Double)
                RecibosCaja.Direction = ParameterDirection.Output
                Dim VentaCont As OracleParameter = cmd.Parameters.Add("PVENTACONTADO", OracleDbType.Double)
                VentaCont.Direction = ParameterDirection.Output
                Try
                    conn.Open()
                    cmd.ExecuteNonQuery()
                    conn.Close()
                    lTotalGastos = Gasto.Value.ToString
                    lTotalRC = RecibosCaja.Value.ToString
                    lTotalConsignaciones = Consigna.Value.ToString
                    lSaldoInicial = SaldoIni.Value.ToString
                    lTotalVentasContado = VentaCont.Value.ToString
                Catch ex As Exception
                    Throw ex
                End Try
            End Using
        End Using
    End Sub
    Public Sub New(ByVal IdCaja As String, ByVal FechaCuadre As Date, ByVal dsCCaja As dsConsultaCartera)
        Try
            iCaja = New cCaja(IdCaja)
            lIdCaja = IdCaja
            lFechaCuadre = FechaCuadre
            lDsRc = dsCCaja
            CargaDatosCuadre()
            GetRecibosCaja()
        Catch ex As Exception
            Throw ex
        End Try
    End Sub
    Public Sub New(ByVal idCaja As String, ByVal dsCCaja As dsConsultaCartera)
        Me.New(idCaja, Date.Today, dsCCaja)
    End Sub

    Public ReadOnly Property FechaUltimoCuadre() As Date
        Get
            Return iCaja.FechaUltimoCuadre
        End Get
    End Property
    Public ReadOnly Property SaldoInicial() As Double
        Get
            Return lSaldoInicial
        End Get
    End Property
    Public ReadOnly Property TotalConsignaciones() As Double
        Get
            Return lTotalConsignaciones
        End Get
    End Property
    Public ReadOnly Property TotalGastos() As Double
        Get
            Return lTotalGastos
        End Get
    End Property
    Public ReadOnly Property TotalRecibosCaja() As Double
        Get
            Return lTotalRC
        End Get
    End Property
    Public ReadOnly Property TotalVentasContado() As Double
        Get
            Return lTotalVentasContado
        End Get
    End Property
    Public ReadOnly Property SaldoCaja() As Double
        Get
            Return SaldoInicial + TotalRecibosCaja - TotalGastos - TotalConsignaciones
        End Get
    End Property
    Public Sub CierreCaja()
        Try
            iCaja.Saldo = SaldoCaja
            iCaja.FechaUltimoCuadre = lFechaCuadre
            iCaja.Salvar()
        Catch ex As Exception
            Throw ex
        End Try
    End Sub
End Class